package cn.gmcx.lazy.izze.controller;


import cn.gmcx.lazy.izze.enums.DynamicTypeEnum;
import cn.gmcx.lazy.izze.pojo.Dynamic;
import cn.gmcx.lazy.izze.pojo.Praise;
import cn.gmcx.lazy.izze.pojo.Video;
import cn.gmcx.lazy.izze.pojo.VideoVo;
import cn.gmcx.lazy.izze.service.FileService;
import cn.gmcx.lazy.izze.service.IDynamicService;
import cn.gmcx.lazy.izze.service.IPraiseService;
import cn.gmcx.lazy.izze.service.IVideoService;
import cn.gmcx.lazy.izze.utils.DateSumUtils;
import cn.gmcx.lazy.izze.utils.Result;
import cn.hutool.core.io.resource.MultiFileResource;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "视频控制层")
@RestController
@RequestMapping("/video")
public class VideoController {

    @Autowired
    private IVideoService videoService;


    @Operation(description = "添加视频")
    @PostMapping("/add_video")
    public Result add( MultipartFile file, Integer userId,Integer type,String synopsis) {
        Integer addIsOk = videoService.add(file, userId, type, synopsis);
        if (addIsOk != 0){
            return new Result(200,"视频发布成功");

        }
        return new Result(500,"视频发布错误，请联系管理员");
    }
    @PostMapping("/video_get_new")
    public Result getNewVideo(Integer id) {
        Page<Video> videoPage = Page.of(1, 8);
        QueryWrapper<Video> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", id);
        wrapper.orderByDesc("create_time");
        Page<Video> page = videoService.page(videoPage, wrapper);
        List<Video> records = page.getRecords();
        return Result.success(records);
    }

    @PostMapping("/video_select_id")
    public Result selectById(Integer id) {
        QueryWrapper<Video> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", id);
        List<Video> list = videoService.list(wrapper);
        return new Result(200, "success", list);
    }

    @PostMapping("/video_select_id_order")
    public Result selectByIdOder(Integer id, String order) {
        QueryWrapper<Video> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", id);
        wrapper.orderByDesc(order);
        Page<Video> videoPage = Page.of(1, 20);
        Page<Video> list = videoService.page(videoPage, wrapper);
        return new Result(200, "success", list);
    }

    @PostMapping("/video_select_id_date")
    public Result selectByIdAndDate(Integer id, @RequestParam(required = false) String start, @RequestParam(required = false) String end, @RequestParam(defaultValue = "1", required = false) Integer page) {
        Map<String, Object> map = new HashMap<>();
        QueryWrapper<Video> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", id);
        if (start != null && end != null) {
            start = DateSumUtils.DateMinus(start, 1L);
            end = DateSumUtils.DateSum(end, 1L);
            wrapper.between("create_time", start, end);
        }
        wrapper.orderByDesc("praise");
        Page<Video> pageOf = Page.of(page, 5);
        Page<Video> videoList = videoService.page(pageOf, wrapper);
        QueryWrapper<Video> totalWrapper = new QueryWrapper<>();
        totalWrapper.eq("user_id", id);
        long videoTotal = videoService.count(totalWrapper);
        map.put("pageTotal", videoList.getPages());
        map.put("list", videoList);
        map.put("videoTotal", videoTotal);
        return new Result(200, "success", map);
    }

    @GetMapping("/video_select_recommend")
    public Result selectRecommend(Integer id) {
        List<VideoVo> videos = videoService.selectRecommend(id);

        return Result.success(videos);
    }

    @PostMapping("/video_update_praise")
    public Result updatePraise(Integer userId,Integer videoId,Boolean isPraise){
        System.out.println(userId+ "/"+videoId+isPraise);
       videoService.updatePraise(userId,videoId,isPraise);
       return Result.success(null);
    }



}
